<?php

namespace App\Modules\Manage\Http\Controllers;

use App\Http\Controllers\ManageController;
use App\Modules\Manage\Model\InviteManageModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Cache;
use Validator;

class inviteManageController extends ManageController
{
    public function __construct()
    {
        parent::__construct();

        $this->initTheme('manage');
        $this->theme->setTitle('招聘管理');
        $this->theme->set('manageType', 'auth');
    }

    public function index(Request $request)
    {
        $merge = $request->all();
        $list = InviteManageModel::select('*')->paginate(8);
        $listArr = $list->toArray()['data'];
        $data = array(
            'merge' => $merge,
            'listArr' => $listArr,
            'list' => $list
        );
        return $this->theme->scope('manage.inviteManagerList',$data)->render();
    }

    public function add()
    {
        $data = [
            'action'=> 'manage/inviteManageAdd'
        ];
        return $this->theme->scope('manage.inviteManageAdd', $data)->render();
    }

    public function postAdd(Request $request)
    {
        $data = $request->except('_token');
        if(empty($data['position']))
        {
            return redirect()->back()->with(['error'=> '招聘岗位不能为空']);
        }
        $res = InviteManageModel::create($data);
        if($res)
        {
            return redirect('manage/inviteManage')->with(['message'=> '添加成功!']);
        }
        return redirect()->back()->with(['error'=> '添加失败,请重试! ']);
    }

    public function delete($id)
    {
        $info = InviteManageModel::where('id', $id)->first();
        if(!empty($info))
        {
            $res = $info->update(['is_delete'=> 1]);
            if($res)
            {
                return redirect()->to('manage/inviteManage')->with(['message'=> '删除成功!']);
            }
        }
        return redirect()->back()->with(['error'=> '删除失败,请重试!']);
    }

    public function edit($id)
    {
        $info = InviteManageModel::where('id', $id)->first();
        if(empty($info))
        {
            return redirect()->back()->with(['error'=> '参数错误!']);
        }
        $info = $info->toArray();
        $data = [
            'info' => $info,
            'action' => 'manage/inviteManageEdit/'.$info['id']
        ];
        return $this->theme->scope('manage.inviteManageAdd', $data)->render();
    }
    public function postEdit(Request $request,$id)
    {
        $new_data = $request->except('_token');
        $info = InviteManageModel::where('id', $id)->first();
        if(!empty($info))
        {
            $res = $info->update($new_data);
            if($res)
            {
                return  redirect()->to('manage/inviteManage')->with(['message'=> '修改成功']);
            }
        }
        return redirect()->back()->with(['error'=> '修改失败, 请重试!']);
    }
}